package vn.aptech.scrumgroup.ui;

import java.util.ArrayList;
import java.util.List;

import vn.aptech.scrumgroup.R;
import vn.aptech.scrumgroup.activity.ConceptActivity;
import vn.aptech.scrumgroup.model.AbstractBaseModel;
import vn.aptech.scrumgroup.model.BacklogModel;
import vn.aptech.scrumgroup.model.DailyScrumModel;
import vn.aptech.scrumgroup.model.ObjectSprintModel;
import vn.aptech.scrumgroup.model.PSIModel;
import vn.aptech.scrumgroup.model.PlanningObjectModel;
import vn.aptech.scrumgroup.model.RetrospectiveModel;
import vn.aptech.scrumgroup.model.ReviewModel;
import vn.aptech.scrumgroup.resource.Resources;
import vn.aptech.scrumgroup.util.MenuUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Unit;

public class EventView extends View{
	
	List<AbstractBaseModel> modelList;
	BacklogModel productBacklog;
	BacklogModel sprintBackLog;
	PlanningObjectModel planningModel;
	ObjectSprintModel sprintModel;
	DailyScrumModel dailyScrumModel;
	PSIModel psiModel;
	ReviewModel reviewModel;
	RetrospectiveModel retrospectiveModel;
	Intent intent;
	Context context;

	public EventView(Context context) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
		init();
	}
	
	private void init() {

		intent = new Intent();
		// TODO Auto-generated method stub
		modelList = new ArrayList<AbstractBaseModel>();
		
		MenuUtil menu = new MenuUtil(context);
		modelList.add(menu);
		
		productBacklog = new BacklogModel(15*Unit.j, 90*Unit.j, 60*Unit.j, 100*Unit.j, 15, "Features", new String[]{"Product","Backlog"}, Color.rgb(51, 122, 162), Color.WHITE, Color.WHITE, Color.WHITE);
//		productBacklog.getRectanglePaint().setARGB(255, 51, 122, 162);
//		
//		productBacklog.getTextPaint().setColor(Color.WHITE);
//		productBacklog.getTextPaint().setTextSize(15);
//		
//		productBacklog.getFooterPaint().setStyle(Style.STROKE);
//		productBacklog.getFooterPaint().setARGB(255, 51, 122, 162);
//		productBacklog.getFooterPaint().setStrokeWidth(1*Unit.j);
//		
//		productBacklog.getLinePaint().setColor(Color.WHITE);
		
		productBacklog.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(productBacklog.getPressState()){
					productBacklog.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.PRODUCT_BACKLOG);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.PRODUCT_BACKLOG));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					productBacklog.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(productBacklog.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					productBacklog.isBrilliant = true;
					productBacklog.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		//productBacklog.getFooterPaint().setStrokeWidth(1.5f*Unit.j);
		modelList.add(productBacklog);
		//Color.RED
		planningModel = new PlanningObjectModel(125*Unit.j, 145*Unit.j, 35*Unit.j, Color.rgb(51, 122, 162), Color.BLACK);
		planningModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(planningModel.getPressState()){
					planningModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle planningBundle = new Bundle();
						planningBundle.putString("resourceId", Resources.SPRINT_PLANNING_MEETING);
						planningBundle.putString("title", context.getResources().getString(R.string.SPRINT_PLANNING_MEETING));
						intent.replaceExtras(planningBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					planningModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(planningModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					planningModel.isBrilliant = true;
					planningModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(planningModel);
		
//		Color.rgb(51, 122, 162)
		sprintBackLog = new BacklogModel(170*Unit.j, 110*Unit.j, 40*Unit.j, 60*Unit.j, 10, "Task", new String[]{"Sprint","Backlog"}, Color.rgb(51, 122, 162), Color.WHITE, Color.WHITE, Color.WHITE);
//		sprintBackLog.getRectanglePaint().set(productBacklog.getRectanglePaint());
//		sprintBackLog.getTextPaint().set(productBacklog.getTextPaint());
//		sprintBackLog.getTextPaint().setColor(Color.WHITE);
//		sprintBackLog.getFooterPaint().set(productBacklog.getFooterPaint());
//		sprintBackLog.getFooterPaint().setARGB(255, 51, 122, 162);
//		sprintBackLog.getLinePaint().set(productBacklog.getLinePaint());
		sprintBackLog.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(sprintBackLog.getPressState()){
					sprintBackLog.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle sprintBundle = new Bundle();
						sprintBundle.putString("resourceId", Resources.SPRINT_BACKLOG);
						sprintBundle.putString("title", context.getResources().getString(R.string.SPRINT_BACKLOG));
						intent.replaceExtras(sprintBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					sprintBackLog.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(sprintBackLog.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					sprintBackLog.isBrilliant = true;
					sprintBackLog.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(sprintBackLog);
		//Color.RED
		sprintModel = new ObjectSprintModel(275*Unit.j, 100*Unit.j, 50*Unit.j, Color.rgb(51, 122, 162), Color.BLACK);
		sprintModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(sprintModel.getPressState()){
					sprintModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle sprintBundle = new Bundle();
						sprintBundle.putString("resourceId", Resources.SPRINT);
						sprintBundle.putString("title", context.getResources().getString(R.string.SPRINT));
						intent.replaceExtras(sprintBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					sprintModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(sprintModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					sprintModel.isBrilliant = true;
					sprintModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(sprintModel);
		
		dailyScrumModel = new DailyScrumModel(new Point((int)(400*Unit.j), (int)(40*Unit.j)), (int)(5*Unit.j), (int)(10*Unit.j), 1, Color.rgb(51, 122, 162), Color.BLACK);
		dailyScrumModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(dailyScrumModel.getPressState()){
					dailyScrumModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle dailyScrumBundle = new Bundle();
						dailyScrumBundle.putString("resourceId", Resources.DAILY_SCRUM);
						dailyScrumBundle.putString("title", context.getResources().getString(R.string.DAILY_SCRUM));
						intent.replaceExtras(dailyScrumBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					dailyScrumModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(dailyScrumModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					dailyScrumModel.isBrilliant = true;
					dailyScrumModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(dailyScrumModel);
		
		psiModel = new PSIModel(new Point((int)(400*Unit.j), (int)(155*Unit.j)), (int)(20*Unit.j), (int)(10*Unit.j), 1, Color.rgb(51, 122, 162), Color.BLACK);
		psiModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(psiModel.getPressState()){
					psiModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle psiBundle = new Bundle();
						psiBundle.putString("resourceId", Resources.INCREMENT);
						psiBundle.putString("title", context.getResources().getString(R.string.INCREMENT));
						intent.replaceExtras(psiBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					psiModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(psiModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					psiModel.isBrilliant = true;
					psiModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(psiModel);
		
		reviewModel = new ReviewModel(new Point((int)(425*Unit.j), (int)(160*Unit.j)), (int)(5*Unit.j), (int)(10*Unit.j), 1, Color.rgb(51, 122, 162), Color.BLACK);
		reviewModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(reviewModel.getPressState()){
					reviewModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle reviewBundle = new Bundle();
						reviewBundle.putString("resourceId", Resources.REVIEW);
						reviewBundle.putString("title", context.getResources().getString(R.string.REVIEW));
						intent.replaceExtras(reviewBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					reviewModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(reviewModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					reviewModel.isBrilliant = true;
					reviewModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(reviewModel);
		
		retrospectiveModel = new RetrospectiveModel(new Point((int)(425*Unit.j), (int)(220*Unit.j)), (int)(5*Unit.j), (int)(10*Unit.j), 1, Color.rgb(51, 122, 162), Color.BLACK);
		retrospectiveModel.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(retrospectiveModel.getPressState()){
					retrospectiveModel.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle reviewBundle = new Bundle();
						reviewBundle.putString("resourceId", Resources.RETROSPECTIVE);
						reviewBundle.putString("title", context.getResources().getString(R.string.RETROSPECTIVE));
						intent.replaceExtras(reviewBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					retrospectiveModel.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(retrospectiveModel.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					retrospectiveModel.isBrilliant = true;
					retrospectiveModel.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(retrospectiveModel);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		//canvas.rotate(90);
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		//canvas.rotate(90);
		for (AbstractBaseModel model:modelList){
			model.onDraw(canvas);
		}//end for
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		for(AbstractBaseModel model : modelList){
			model.fireTouchEvent(event);
		}
		this.invalidate();
		return true;
	}
	
	
}
